﻿using Microsoft.AspNetCore.Cors;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Newtonsoft.Json.Linq;
using StartEAS_API.Utils;
using StartsEAS_API.BLL.Interfaces.ICourseB;
using StartsEAS_API.Model;
using StartsEAS_API.Model.Models;

namespace StartsEAS_API.Controllers.CourseContor
{
    [Route("api/[controller]/[action]")]
    [ApiController]
    [EnableCors("any")]
    public class CourseController : ControllerBase
    {

        private readonly ICourseBLL _courseBll;

        public CourseController(ICourseBLL courseBll)
        {
            _courseBll = courseBll;
        }
        /// <summary>
        /// 查询所有课程信息
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public ResultUtils GetAllCourse() {
            try
            {
                return _courseBll.GetAllCourse();
            }
            catch (System.Exception e)
            {
                AppLog.WriteError($"GetAllCourse服务器内部异常：{e}", true);
                return new ResultUtils() { code = 500, message = $"服务器内部异常\n{e.Message}" };
            }
        }
        /// <summary>
        /// 新增课程信息列表
        /// </summary>
        /// <param name="data">课程信息列表</param>
        /// <returns></returns>
        [HttpPost]
        public ResultUtils InsertCourse(JArray data){
            try
            {
                return _courseBll.InsertCourse(data);
            }
            catch (System.Exception e)
            {
                AppLog.WriteError($"InsertCourse服务器内部异常：{e}", true);
                return new ResultUtils() { code = 500, message = $"服务器内部异常\n{e.Message}" };
            }
        }
        /// <summary>
        /// 修改课程信息
        /// </summary>
        /// <param name="data">被修改的课程信息实体</param>
        /// <returns></returns>
        [HttpPut]
        public ResultUtils UpdateCourse(Course data) {
            try
            {
                return _courseBll.UpdataCourse(data);
            }
            catch (System.Exception e)
            {
                AppLog.WriteError($"UpdateCourse服务器内部异常：{e}", true);
                return new ResultUtils() { code = 500, message = $"服务器内部异常\n{e.Message}" };
            }
        }

    }
}
